﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.config;
using com.lover.astd.common.model;

namespace com.lover.astd.common.manager
{
    public class SettingsMgr
    {
        private GameConfig _conf;
        private User _user;
        private List<ISettings> _settings = new List<ISettings>();
        public SettingsMgr(GameConfig conf, User u) { _conf = conf; _user = u; }
        private ISettings findSetting(string setting_name) { for (int i = 0; i < _settings.Count; i++) { if (_settings[i].getName().Equals(setting_name)) { return _settings[i]; } } return null; }
        public void addSetting(ISettings setting)
        {
            if (setting == null || setting.getName() == null) return;
            //first check if added already, if so, return , else add
            if (findSetting(setting.getName()) != null) return;
            _settings.Add(setting);
        }
        
        public void renderSettings()
        {
            string ui_key = _user.popUiFromQueue();
            while (ui_key != null && ui_key != "")
            {
                ISettings setting = findSetting(ui_key);
                if (setting != null) setting.renderSettings();
                ui_key = _user.popUiFromQueue();
            }
        }
        public void init()
        {
            for (int i = 0; i < _settings.Count; i++)
            {
                ISettings setting = _settings[i];
                if (setting == null) continue;
                setting.renderSettings();
            }
        }
        public void saveSettings()
        {
            for (int i = 0; i < _settings.Count; i++)
            {
                ISettings setting = _settings[i];
                if (setting == null) continue;
                setting.saveSettings();
            }
        }
        public void loadDefaultSettings()
        {
            for (int i = 0; i < _settings.Count; i++)
            {
                ISettings setting = _settings[i];
                if (setting == null) continue;
                setting.loadDefaultSettings();
            }
        }
    }
}
